package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblQueryFieldConfig;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblQueryFieldConfigService;
import com.settlement.system.common.util.common.FasUtil;
import com.alibaba.excel.context.AnalysisContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 联表查询字段配置表 前端控制器
 * @author xiaochuan
 * @since 2022-01-13
 */

@Slf4j
@Api(tags = "联表查询字段配置表",description = "联表查询字段配置表")
@RestController
@RequestMapping("/tblQueryFieldConfig")
public class TblQueryFieldConfigController extends ExcelController<TblQueryFieldConfig> {

    @Resource
    private ITblQueryFieldConfigService tblQueryFieldConfigService;

    @Override
    public IBaseService init() {
        return tblQueryFieldConfigService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblQueryFieldConfig.class;
    }

    @Override
    public String verifyImportObject(TblQueryFieldConfig modelType, AnalysisContext analysisContext) {
        String message = super.verifyImportObject(modelType, analysisContext);
        if (StringUtils.isNotBlank(message)) {
            return message;
        }
        Map<String, Object> params = new HashMap<String, Object>(16);
        params.put("tableNameZn", modelType.getTableNameZn());
        params.put("columnNameZn", modelType.getColumnNameZn());
        try {
            int count = tblQueryFieldConfigService.selectCount(params);
            if (count > 0) {
                message = "存在重复数据，请核实。";
                return message;
            }
        } catch (ServiceException e) {
            message = "基础配置-条件配置导入，查询是否重复异常";
            e.printStackTrace();
            log.error(message, e);
            return message;
        }
        return null;
    }

    @ApiOperation("刷新")
    @RequestMapping(value = "refresh", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse refresh(@RequestBody Map<String, Object> model, HttpServletRequest request) {
        try {
            boolean empty = isEmpty(model);
            if(!empty){
                tblQueryFieldConfigService.refresh(model);
            }
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_SUCCESS);
        } catch (Exception ex) {
            log.info("更新数据异常", ex);
            return new ObjectDataResponse(ReturnStatusEnum.REQUEST_ABNORMAL);
        }
    }

    private boolean isEmpty(Map<String, Object> model){
        boolean b = model.entrySet().stream().allMatch(s -> {
            String value = (String) s.getValue();
            return StringUtils.isEmpty(value);
        });
        return b;
    }

    @ApiOperation("获取表名对应的字段名称列表")
    @RequestMapping(value = "pullColumnNameList", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse pullColumnNameList(HttpServletRequest request, @RequestBody Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model,"tableName");
        return tblQueryFieldConfigService.pullColumnNameList(model);
    }
}